# XDevice Build System

CC = gcc
CXX = g++
CFLAGS = -Wall -Wextra -O3 -std=c11 -pthread -fPIC -D_GNU_SOURCE
CXXFLAGS = -Wall -Wextra -O3 -std=c++17 -pthread -fPIC
LDFLAGS = -pthread -lrt -lm

# 目录结构
SRCDIR = src
INCDIR = include
BUILDDIR = build
BINDIR = bin
TESTDIR = tests

# 源文件
CORE_SOURCES = $(wildcard $(SRCDIR)/core/*.c)
RAFT_SOURCES = $(wildcard $(SRCDIR)/raft/*.c)
CONSENSUS_SOURCES = $(wildcard $(SRCDIR)/consensus/*.c)
STORAGE_SOURCES = $(wildcard $(SRCDIR)/storage/*.c)
BACKEND_SOURCES = $(wildcard $(SRCDIR)/storage/backends/*.c)
TRANSPORT_SOURCES = $(wildcard $(SRCDIR)/transport/*.c)
RPC_SOURCES = $(wildcard $(SRCDIR)/rpc/*.c)
UTILS_SOURCES = $(wildcard $(SRCDIR)/utils/*.c)
MAIN_SOURCES = $(wildcard $(SRCDIR)/main/*.c)

ALL_SOURCES = $(CORE_SOURCES) $(RAFT_SOURCES) $(CONSENSUS_SOURCES) $(STORAGE_SOURCES) \
              $(BACKEND_SOURCES) $(TRANSPORT_SOURCES) $(RPC_SOURCES) $(UTILS_SOURCES) \
              src/libxdevice.c

# 对象文件
OBJECTS = $(patsubst $(SRCDIR)/%.c,$(BUILDDIR)/%.o,$(ALL_SOURCES))

# 可执行文件
XDEVICE_NODE = $(BINDIR)/xdevice-node
XDEVICE_CLI = $(BINDIR)/xdevice-cli
XDEVICE_TEST = $(BINDIR)/xdevice-test
XDEVICE_MONITOR = $(BINDIR)/xdevice-monitor

# 测试文件
TEST_BASIC = $(BINDIR)/test_library_basic
TEST_INTEGRATED_RPC = $(BINDIR)/test_integrated_rpc
TEST_MULTI_THREAD = $(BINDIR)/test_raft_multi_thread
TEST_MULTI_PROCESS = $(BINDIR)/test_raft_multi_process
TEST_THREE_REPLICA = $(BINDIR)/test_three_replica_data
TEST_RAFT_MEMBERSHIP = $(BINDIR)/test_raft_membership
TEST_REPLICATION = $(BINDIR)/replication_test
TEST_ENHANCED_REPLICA = $(BINDIR)/enhanced_replica_test

# 库文件
LIBXDEVICE = $(BINDIR)/libxdevice.so

# 默认目标
all: dirs $(LIBXDEVICE) $(XDEVICE_NODE) $(XDEVICE_CLI) $(XDEVICE_MONITOR) tests

# 创建目录
dirs:
	@mkdir -p $(BUILDDIR)/core
	@mkdir -p $(BUILDDIR)/raft
	@mkdir -p $(BUILDDIR)/consensus
	@mkdir -p $(BUILDDIR)/storage
	@mkdir -p $(BUILDDIR)/storage/backends
	@mkdir -p $(BUILDDIR)/rpc
	@mkdir -p $(BUILDDIR)/transport
	@mkdir -p $(BUILDDIR)/utils
	@mkdir -p $(BUILDDIR)/main
	@mkdir -p $(BINDIR)

# 编译对象文件
$(BUILDDIR)/%.o: $(SRCDIR)/%.c
	$(CC) $(CFLAGS) -I$(INCDIR) -c $< -o $@

# 编译库文件
$(LIBXDEVICE): $(OBJECTS)
	$(CC) $(LDFLAGS) -shared -o $@ $^

# 编译主程序
$(XDEVICE_NODE): $(SRCDIR)/main/node_main.c $(LIBXDEVICE)
	$(CC) $(CFLAGS) -I$(INCDIR) -L$(BINDIR) $< -lxdevice -o $@

$(XDEVICE_CLI): $(SRCDIR)/main/cli_main.c $(LIBXDEVICE)
	$(CC) $(CFLAGS) -I$(INCDIR) -L$(BINDIR) $< -lxdevice -o $@

$(XDEVICE_MONITOR): tools/xdevice-monitor.c $(LIBXDEVICE)
	$(CC) $(CFLAGS) -I$(INCDIR) -L$(BINDIR) $< -lxdevice -o $@

# 测试目标
tests: $(TEST_BASIC) $(TEST_INTEGRATED_RPC) $(TEST_MULTI_THREAD) $(TEST_MULTI_PROCESS) $(TEST_THREE_REPLICA) $(TEST_RAFT_MEMBERSHIP) $(TEST_REPLICATION) $(TEST_ENHANCED_REPLICA)

# 基础库功能测试
$(TEST_BASIC): $(TESTDIR)/test_library_basic.c $(LIBXDEVICE)
	$(CC) $(CFLAGS) -I$(INCDIR) -o $@ $< -L$(BINDIR) -lxdevice

# 集成RPC测试
$(TEST_INTEGRATED_RPC): $(TESTDIR)/test_integrated_rpc.c $(LIBXDEVICE)
	$(CC) $(CFLAGS) -I$(INCDIR) -o $@ $< -L$(BINDIR) -lxdevice

# 多线程测试
$(TEST_MULTI_THREAD): $(TESTDIR)/test_raft_multi_thread.c $(TESTDIR)/test_helpers.c $(LIBXDEVICE)
	$(CC) $(CFLAGS) -I$(INCDIR) -o $@ $(TESTDIR)/test_raft_multi_thread.c $(TESTDIR)/test_helpers.c -L$(BINDIR) -lxdevice

# 多进程测试
$(TEST_MULTI_PROCESS): $(TESTDIR)/test_raft_multi_process.c $(TESTDIR)/test_helpers.c $(LIBXDEVICE)
	$(CC) $(CFLAGS) -I$(INCDIR) -o $@ $(TESTDIR)/test_raft_multi_process.c $(TESTDIR)/test_helpers.c -L$(BINDIR) -lxdevice

# 三副本数据复制测试
$(TEST_THREE_REPLICA): $(TESTDIR)/test_three_replica_data.c $(LIBXDEVICE)
	$(CC) $(CFLAGS) -I$(INCDIR) -o $@ $< -L$(BINDIR) -lxdevice

# Raft 组成员测试
$(TEST_RAFT_MEMBERSHIP): $(TESTDIR)/test_raft_membership.c $(LIBXDEVICE)
	$(CC) $(CFLAGS) -I$(INCDIR) -o $@ $< -L$(BINDIR) -lxdevice

# 分布式复制测试
$(TEST_REPLICATION): examples/replication_test.c $(LIBXDEVICE)
	$(CC) $(CFLAGS) -I$(INCDIR) -o $@ $< -L$(BINDIR) -lxdevice

# 增强型副本一致性测试
$(TEST_ENHANCED_REPLICA): examples/enhanced_replica_test.c $(LIBXDEVICE)
	$(CC) $(CFLAGS) -I$(INCDIR) -o $@ $< -L$(BINDIR) -lxdevice

# 高级测试目标 - 只保留核心测试
RAFT_MULTI_THREAD_TEST = $(TEST_MULTI_THREAD)
RAFT_MULTI_PROCESS_TEST = $(TEST_MULTI_PROCESS)

# 测试分组
CORE_TESTS = $(RAFT_MULTI_THREAD_TEST) $(RAFT_MULTI_PROCESS_TEST) $(TEST_THREE_REPLICA) $(TEST_RAFT_MEMBERSHIP)
ALL_TESTS = $(CORE_TESTS) $(TEST_BASIC) $(TEST_INTEGRATED_RPC)

# 基础功能测试
test-basic: $(TEST_BASIC)
	@echo "Running basic library functionality test..."
	export LD_LIBRARY_PATH=./bin:$$LD_LIBRARY_PATH && ./$(TEST_BASIC)

# 集成RPC测试
test-integrated: $(TEST_INTEGRATED_RPC) $(XDEVICE_MONITOR)
	@echo "Running integrated test with RPC monitoring..."
	@echo "This test provides RPC interface for monitoring."
	@echo "You can monitor it in another terminal using:"
	@echo "  ./bin/xdevice-monitor -s /tmp/xdevice_test.sock status"
	@echo "  ./bin/xdevice-monitor -s /tmp/xdevice_test.sock monitor"
	export LD_LIBRARY_PATH=./bin:$$LD_LIBRARY_PATH && ./$(TEST_INTEGRATED_RPC)

# 快速测试 - 运行多线程测试（非交互式）
test: $(RAFT_MULTI_THREAD_TEST)
	@echo "Running quick multi-thread cluster test..."
	@echo "This will run a 5-second non-interactive test:"
	export LD_LIBRARY_PATH=./bin:$$LD_LIBRARY_PATH && ./$(RAFT_MULTI_THREAD_TEST) -d 5

# 三副本数据复制测试
test-replica: $(TEST_THREE_REPLICA)
	@echo "Running three-replica data replication test..."
	@echo "This test verifies that data written via xDevice API"
	@echo "is automatically replicated to three local files."
	export LD_LIBRARY_PATH=./bin:$$LD_LIBRARY_PATH && ./$(TEST_THREE_REPLICA)

# 分布式复制测试
test-replication: $(TEST_REPLICATION)
	@echo "Running distributed replication test..."
	@echo "This test performs continuous write operations and verifies data consistency across replicas."
	export LD_LIBRARY_PATH=./bin:$$LD_LIBRARY_PATH && ./$(TEST_REPLICATION)

# 增强型副本一致性测试
test-enhanced-replica: $(TEST_ENHANCED_REPLICA)
	@echo "Running enhanced replica consistency test..."
	@echo "This test performs comprehensive replica verification with concurrent reads and deep consistency checks."
	export LD_LIBRARY_PATH=./bin:$$LD_LIBRARY_PATH && ./$(TEST_ENHANCED_REPLICA)

# 运行所有核心测试
test-all: $(ALL_TESTS)
	@echo "Running all tests..."
	@echo "1. Basic library functionality test..."
	export LD_LIBRARY_PATH=./bin:$$LD_LIBRARY_PATH && ./$(TEST_BASIC)
	@echo "2. Multi-thread cluster test (5 seconds)..."
	export LD_LIBRARY_PATH=./bin:$$LD_LIBRARY_PATH && ./$(RAFT_MULTI_THREAD_TEST) -d 5
	@echo "3. Multi-process cluster test (5 seconds)..."
	export LD_LIBRARY_PATH=./bin:$$LD_LIBRARY_PATH && ./$(RAFT_MULTI_PROCESS_TEST) -d 5
	@echo "4. Three-replica data replication test..."
	export LD_LIBRARY_PATH=./bin:$$LD_LIBRARY_PATH && ./$(TEST_THREE_REPLICA)
	@echo "All tests completed."

# 安装依赖
deps:
	@echo "Installing dependencies..."
	# Ubuntu/Debian
	@if command -v apt-get >/dev/null 2>&1; then \
		sudo apt-get update && \
		sudo apt-get install -y build-essential libevent-dev libssl-dev \
		zlib1g-dev liblz4-dev libzstd-dev uuid-dev pkg-config; \
	fi
	# CentOS/RHEL
	@if command -v yum >/dev/null 2>&1; then \
		sudo yum groupinstall -y "Development Tools" && \
		sudo yum install -y libevent-devel openssl-devel \
		zlib-devel lz4-devel libzstd-devel libuuid-devel pkgconfig; \
	fi

# 检查依赖
check-deps:
	@./scripts/check-dependencies.sh

# 清理
clean:
	rm -rf $(BUILDDIR)
	rm -rf $(BINDIR)
	rm -rf /tmp/xdevice_test_*
	rm -rf /tmp/xdevice_raft_test_*

# 安装
install: all
	sudo mkdir -p /usr/local/bin
	sudo mkdir -p /usr/local/lib
	sudo mkdir -p /usr/local/include/xdevice
	sudo cp $(BINDIR)/xdevice-* /usr/local/bin/
	sudo cp $(LIBXDEVICE) /usr/local/lib/
	sudo cp -r $(INCDIR)/* /usr/local/include/xdevice/
	sudo ldconfig

# 卸载
uninstall:
	sudo rm -f /usr/local/bin/xdevice-*
	sudo rm -f /usr/local/lib/libxdevice.so
	sudo rm -rf /usr/local/include/xdevice

# 显示帮助信息
help:
	@echo "XDevice Build System - 可用目标:"
	@echo ""
	@echo "构建目标:"
	@echo "  all              - 构建所有主要组件 (库文件和主程序)"
	@echo "  dirs             - 创建构建目录"
	@echo "  clean            - 清理构建文件和临时文件"
	@echo ""
	@echo "测试目标:"
	@echo "  test             - 运行快速测试 (多线程集群测试)"
	@echo "  test-all         - 运行所有核心测试"
	@echo "  test-multi-thread   - 运行多线程真实集群测试 🧵"
	@echo "  test-multi-process  - 运行多进程真实集群测试 🚀"
	@echo "  test-replica        - 运行三副本数据复制测试 📂"
	@echo ""
	@echo "安装目标:"
	@echo "  install          - 安装到系统目录"
	@echo "  uninstall        - 从系统目录卸载"
	@echo "  deps             - 安装编译依赖"
	@echo "  check-deps       - 检查依赖是否满足"
	@echo ""
	@echo "示例用法:"
	@echo "  make             - 构建项目"
	@echo "  make test        - 运行快速测试"
	@echo "  make test-all    - 运行所有核心测试"
	@echo "  make test-multi-thread  - 启动多线程集群测试控制台"
	@echo ""
.PHONY: all dirs test test-all test-multi-process test-multi-thread test-replica clean install uninstall deps check-deps help